using I2.Loc;
using UnityEngine;

public class ManagerSelectMap : MonoBehaviour
{
	public bool LoadLevel;

	public MapInfo[] arrMapInfo;

	public GameObject randomArrow;

	public MapInfo ExampleButton;

	private UIGrid _gridMap;

	public void Start()
	{
		_gridMap = GetComponent<UIGrid>();
		_gridMap.enabled = true;
		InstantiateLevelsScroll();
		arrMapInfo = GetComponentsInChildren<MapInfo>(true);
		SortMaps();
	}

	public void OnEnable()
	{
		if (_gridMap != null)
		{
			SortMaps(true);
		}
		if (randomArrow != null)
		{
			randomArrow.SetActive(false);
			if (ManagerMissions.currMission != null && !ManagerMissions.currMission.offlineMission && string.IsNullOrEmpty(ManagerMissions.currMission.nameScene))
			{
				randomArrow.SetActive(true);
				ManagerMissions.currMission = null;
			}
		}
		Invoke("DisableBug", 1.5f);
	}

	public void DisableBug()
	{
		if (ReplacePerelivController.instance != null)
		{
			ReplacePerelivController.instance.pvpMapsScreenDelay = false;
		}
	}

	private void OnDisable()
	{
		MapInfo[] array = arrMapInfo;
		foreach (MapInfo mapInfo in array)
		{
			mapInfo.transform.localPosition = Vector3.zero;
		}
	}

	private void SortMaps(bool needInit = false)
	{
		if (needInit && ManagerPVPLevels.instance != null)
		{
			ManagerPVPLevels.instance.InitRoomList();
		}
		MapInfo[] array = arrMapInfo;
		foreach (MapInfo mapInfo in array)
		{
			mapInfo.popularity = ManagerPVPLevels.instance.CheckMapPopularity(mapInfo.curLevel);
			if (mapInfo.curLevel.nameScene != "Level1")
			{
				mapInfo.gameObject.name = (int)(4 - mapInfo.popularity) + mapInfo.nameForSort;
			}
			mapInfo.SetPopularityInfo();
		}
		_gridMap.enabled = true;
	}

	public void InstantiateLevelsScroll()
	{
		if (!(ManagerPVPLevels.instance != null))
		{
			return;
		}
		ManagerPVPLevels.instance.InitRoomList();
		foreach (LevelsInfo level in ManagerPVPLevels.instance.levels)
		{
			if (level.modeGame != 0)
			{
				MapInfo component = ((GameObject)Object.Instantiate(ExampleButton.gameObject, _gridMap.transform.position, Quaternion.identity)).GetComponent<MapInfo>();
				component.nameForSort = level.placeInScroll + "_" + level.nameLevel;
				component.curLevel = level;
				string text = ScriptLocalization.Get(string.Format("PVP_map_info/{0}", level.nameScene));
				if (!string.IsNullOrEmpty(text))
				{
					component.curLevel.nameLevel = text;
				}
				component.transform.parent = base.transform;
				component.transform.localScale = Vector3.one;
				component.gameObject.name = (int)(4 - component.popularity) + component.nameForSort;
				component.levelPreview.mainTexture = Resources.Load<Texture>("Textures/LevelPreview/" + level.nameTextureForButton);
				component.gameObject.SetActive(true);
			}
		}
		SortMaps();
	}

	public void SelectMap(MapInfo needMap)
	{
		if (LoadLevel)
		{
			controllerConnectPhoton.thisScript.connectRandomRoom(needMap.curLevel.nameScene, needMap.curLevel.modeGame);
			return;
		}
		MapInfo[] array = arrMapInfo;
		foreach (MapInfo mapInfo in array)
		{
			if (mapInfo == needMap)
			{
				mapInfo.SetSelectionMap(true);
			}
			else if (!LoadLevel)
			{
				mapInfo.SetSelectionMap(false);
			}
		}
	}
}
